//
//  TypeSelectView.m
//  Final
//
//  Created by student on 2021/12/16.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TypeSelectView.h"

@interface TypeSelectView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(strong, nonatomic) UILabel* label;
@property(strong, nonatomic) UICollectionView* cv;
@property(strong, nonatomic) NSMutableArray* typeArr;

@end

@implementation TypeSelectView

-(void) setViews
{
    self.type = 0;
    self.typeArr = [[NSMutableArray alloc]initWithObjects:@"二手闲置", @"打听求助",@"青青子衿",@"瓜田趣事",@"校园招聘",nil];
    
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(80, 20);
    self.cv = [[UICollectionView alloc]initWithFrame:CGRectMake(120, 0, 270, 60) collectionViewLayout:layout];
    self.cv.backgroundColor = [UIColor clearColor];
    [self.cv setDelegate:self];
    [self.cv setDataSource:self];
    [self.cv registerClass:[TypeCell class] forCellWithReuseIdentifier:@"CellView"];
    [self addSubview:self.cv];
    [self addSubview:self.label];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    int ind = (int)indexPath.row;
    TypeCell* cell = (TypeCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"CellView" forIndexPath:indexPath];
    [cell setChoice:self.typeArr[ind]];
    
    if(self.type - 1 == ind){
        [cell selectOn];
    }
    else{
        [cell selectOff];
    }
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    int ind = (int)indexPath.row;
    self.type = ind + 1;
    [self.cv reloadData];
}

//lazy load
-(UILabel*) label
{
    if(_label == nil){
        _label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 150, 20)];
        _label.text = @"请选择类型：";
    }
    return _label;
}


@end
